//
//  YDFVisitorView.swift
//  weibo20
//
//  Created by mac on 16/1/21.
//  Copyright © 2016年 tingyuyisheng. All rights reserved.
//

import UIKit

protocol YDFVisitorViewDelegate:NSObjectProtocol{

    /// 注册方法
    func VisitorViewDidRegister()
    
    /// 登陆方法
    func VisitorViewDidLogin()
}

/// 访客视图处理 -- 用来处理用户未登录的情况
class YDFVisitorView: UIView {

    weak var delegate:YDFVisitorViewDelegate?
    
    // MARK: - 监听方法
    @objc private func clickRegister(){
    
        delegate?.VisitorViewDidRegister()
    }
    
    @objc private func clickLogin(){
    
        delegate?.VisitorViewDidLogin()
    }
    
    
    /// 外界设置首页信息函数
    ///
    /// - parameter imageName: <#imageName description#>
    /// - parameter title:     <#title description#>
	func setupInfo(imageName: String?, title: String) {

		messageLabel.text = title;

		guard let imaName = imageName else {
            startAimation()
			return
		}

		iconView.image = UIImage(named: imaName)
        houseImageView.hidden = true
        sendSubviewToBack(maskImageView)
	}
    
/// 开启首页动画
    private func startAimation(){
    
        // 核心动画
        let aima = CABasicAnimation(keyPath: "transform.rotation")
        aima.toValue = 2 * M_PI
        aima.repeatCount = MAXFLOAT
        aima.duration = 20;
        
        aima.removedOnCompletion = false
        
        // 添加到图层
        iconView.layer.addAnimation(aima, forKey: nil)
    }

	override init(frame: CGRect) {
		super.init(frame: frame)
		setupUI()
	}

	required init?(coder aDecoder: NSCoder) {
		// fatalError("init(coder:) has not been implemented")
		super.init(coder: aDecoder)
		setupUI()
	}

	// MARK: - 懒加载控件
	private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
	// 遮罩
	private lazy var maskImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
	// 小房子
	private lazy var houseImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
	private lazy var messageLabel : UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜")


	// 注册按钮
	private lazy var registerBtn : UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), imageName: "common_button_white_disable")


	// 登陆按钮
	private lazy var loginBtn : UIButton = UIButton(title: "登陆", color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")
}

extension YDFVisitorView {

	/// 设置界面
	private func setupUI() {

		/// 添加控件
		addSubview(iconView)
		addSubview(maskImageView)
		addSubview(houseImageView)
		addSubview(messageLabel)
		addSubview(registerBtn)
		addSubview(loginBtn)
		// 设置自动布局

		// 手动创建约束的时候,需要取消屏蔽frame的布局
		for subView in subviews {

			subView.translatesAutoresizingMaskIntoConstraints = false
		}

		// 设置iconView的布局
		addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
		addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))

		// 设置小房子布局
		addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
		addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
		// 设置文字
		addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
		addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
		addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
		addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
		// 按钮的约束
		// 登陆按钮
		addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1, constant: 0))
		addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
		// If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
		addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
		addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))

		// 注册按钮
		addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1, constant: 0))
		addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
		// If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
		addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
		addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))

		// 遮罩布局
		/**
		 VFL
		 H :水平
		 V :垂直
		 | :边界
		 []:包装控件
		 views : 是一个字典
		 */
		addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskImageView]))
		addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(-36)-[regButton]", options: [], metrics: nil, views: ["mask": maskImageView, "regButton": registerBtn]))
        
        registerBtn.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
        loginBtn.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)
	}
}
